package com.lly.songhigh.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lly.songhigh.R;
import com.lly.songhigh.entity.SingerSingle;
import com.lly.songhigh.utils.ChangeUtils;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 遇博 on 15/10/28.
 */
public class SingerSingleListViewAdapter extends BaseAdapter{
    private Context context;
    private List<SingerSingle.DataEntity> songsEntities;
    private SingerSingle.DataEntity songsEntity;
    private OnMenuClickListener onMenuClickListener;

    public SingerSingleListViewAdapter(Context context, List<SingerSingle.DataEntity> songsEntities) {
        this.context = context;
        this.songsEntities = songsEntities;
    }

    public void setOnMenuClickListener(OnMenuClickListener onMenuClickListener) {
        this.onMenuClickListener = onMenuClickListener;
    }

    public interface OnMenuClickListener{
        void onMenuClick(SingerSingle.DataEntity dataEntity);
    }

    @Override
    public int getCount() {
        return songsEntities != null && songsEntities.size() > 0 ? songsEntities.size() : 0;
    }

    @Override
    public SingerSingle.DataEntity getItem(int position) {
        return songsEntities.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.charts_detail_item, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        songsEntity = getItem(position);
        if (songsEntity != null) {
            String num = ChangeUtils.getCount(songsEntity.getFavorites());
            viewHolder.songsListPosition.setText(position + 1 + "");
            viewHolder.songsListSongName.setText(songsEntity.getName());
            viewHolder.songsListItemSinger.setText(songsEntity.getSingerName());
            viewHolder.songsListItemNumber.setText(num);
            viewHolder.listMore.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onMenuClickListener != null) {
                        onMenuClickListener.onMenuClick(songsEntities.get(position));
                    }
                }
            });
        }
        return convertView;
    }


    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'charts_detail_item.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */
    static class ViewHolder {
        @Bind(R.id.songs_list_position)
        TextView songsListPosition;
        @Bind(R.id.songs_list_songName)
        TextView songsListSongName;
        @Bind(R.id.songs_list_item_singer)
        TextView songsListItemSinger;
        @Bind(R.id.songs_list_item_number)
        TextView songsListItemNumber;
        @Bind(R.id.list_more)
        ImageView listMore;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

}


